
 /*
 * 二开版本 （由于接口貌似PHP所以在此开发了一套Java后台）
 * Author: 郭少
 * gitee: https://gitee.com/guoshao99/tuodan-manghe?_from=gitee_search
 * QQ：2549789059
 * 微信：2549789059
 */


let requestUrl = 'http://127.0.0.1:8085/apimanhe/wx9db338cb8ef9d7e1/'


//let requestUrl = 'http://127.0.0.1:8085/apimanhe/wx761d9a302cb7534a/'


//声明对象
const app = getApp();
//设置全局变量
app.globalData.OperatorIndex = 100;
//获取全局变量
console.log(app.globalData);

function request(url, method,data) {
  return new Promise((reslove, reject) => {
    wx.request({
      url: requestUrl + url,
      method,
      data,
      success(res) {
        reslove(res.data, res);
      }
    })
  })

}

function getCode(data) {
  return new Promise((reslove, reject) => {
    wx.request({
      url: requestUrl + 'wx/login',
      data,
      method: 'GET',
      success(res) {
        reslove(res.data, res);
      }
    })
  })
}

function getConfig(data) {
  return new Promise((reslove, reject) => {
    request('shejiao/getConfig','GET').then(res=>{
      reslove(res)
    })
  })
}

function save(data) {
  return new Promise((reslove, reject) => {
    request('save','GET',data).then(res=>{
      reslove(res)
    })
  })
}
function school(data) {
  return new Promise((reslove, reject) => {
    request('school','GET',data).then(res=>{
      reslove(res)
    })
  })
}

function random(data) {
  return new Promise((reslove, reject) => {
    request('random','GET',data).then(res=>{
      reslove(res)
    })
  })
}

function pay(data) {
  return new Promise((reslove, reject) => {
    request('pay','GET',data).then(res=>{
      reslove(res)
    })
  })
}
/**
 * 获取用户推广信息
 */
function getMemberUser(data) {
  return new Promise((reslove, reject) => {
    request('member/getMemberUser','GET',data).then(res=>{
      reslove(res)
    })
  })
}

/**
 * 获取用户推广信息
 */
function getWxQrCode(data) {
  return new Promise((reslove, reject) => {
    request('member/getWxQrCode','GET',data).then(res=>{
      reslove(res)
    })
  })
}

/**
 * 获取用户推广信息
 */
function saveTeamLevel(data) {
  return new Promise((reslove, reject) => {
    request('teamlevel/save','GET',data).then(res=>{
      reslove(res)
    })
  })
}

function login(e,that){
  console.log(e);
    let userInfo = e.detail.userinfo;
    console.log(userInfo);

    if (e.detail.index == 1) {
      wx.login({
        success(res) {
          if (res.code) {
            //发起网络请求
            wx.showLoading({
              title:'登录中...'
            })
            getCode({
              code: res.code,
              nickName:userInfo.nickName,
              avatarUrl:userInfo.avatarUrl,
              gender:userInfo.gender,
              country:userInfo.country,
              province:userInfo.province,
              city:userInfo.city
            }).then(res => {
              if (res.code == 200) {
                wx.setStorageSync('openid', res.data.openid);
                wx.setStorageSync('newUser', res.data.newUser)
                wx.setStorageSync("userInfo", JSON.stringify(userInfo));
                console.log(wx.getStorageSync('shareCode'));
                if(res.data.newUser){
                  
                  let shareCode = app.globalData.shareCode;
                  let newUser = res.data.newUser;
                  let openid = res.data.openid;
                  saveTeamLevel({shareCode,openid,newUser});
                }
                that.setData({
                  halfShow:false
                })
              }
              wx.hideLoading() 
            })
          } else {
            console.log('登录失败！' + res.errMsg)
            wx.hideLoading() 
          }
        }
      })
    } else {
      that.setData({
        halfShow:false
      })
  
      return;
    }
}


module.exports = {
  request,
  getCode,
  getConfig,
  save,
  school,random,pay,getMemberUser,
  getWxQrCode,
  login,
  saveTeamLevel
};